# /usr/bin/env python3.6
# -*- encoding: utf-8 -*-
"""
@File    : movie_api.py
@Time    : 2020/2/7 0007 10:13
@Author  : zhangbing
@Email   : 18829172841@163.com
@Software: PyCharm
"""
import datetime

from flask_restful import Resource, reqparse, abort, fields, marshal, marshal_with
from werkzeug.datastructures import FileStorage

from App.apis.admin.utils import login_admin_required, required_admin_permission
from App.apis.common.utils import filename_transfer
from App.models.common.movie_model import Movie
from App.setting import UPLOAD_DIR, FILE_PATH_PREFIX

'''
此接口主要用于添加电影相关信息
'''

parese = reqparse.RequestParser()

parese.add_argument("showname", type=str, required=True, help="must supply  showname")
parese.add_argument("shownameen", type=str, required=True, help="must supply  shownameen")
parese.add_argument("director", type=str, required=True, help="must supply  director")
parese.add_argument("leadingRole", type=str, required=True, help="must supply  leadingRole")
parese.add_argument("type", type=str, required=True, help="must supply  type")
parese.add_argument("country", type=str, required=True, help="must supply  country")
parese.add_argument("language", type=str, required=True, help="must supply  language")
parese.add_argument("duraction", type=int, required=True, help="must supply  duraction")
parese.add_argument("screeningmodel", type=str, required=True, help="must supply  screeningmodel")
parese.add_argument("openday", required=True, help="must supply  openday")
parese.add_argument("backgroundpicture", type=FileStorage, required=True, help="must supply  backgroundpicture",
                    location=["files"])

movie_fields = {
    "showname": fields.String,
    "shownameen": fields.String,
    "director": fields.String,
    "leadingRole": fields.String,
    "type": fields.String,
    "country": fields.String,
    "language": fields.String,
    "duraction": fields.String,
    "screeningmodel": fields.String,
    "openday": fields.DateTime,
    "backgroundpicture": fields.String,

}

mutli_movies_fields = {
    "status": fields.Integer,
    "msg": fields.String,
    "data": fields.List(fields.Nested(movie_fields))
}


class MoviesResource(Resource):
    @marshal_with(mutli_movies_fields)
    def get(self):
        movies = Movie.query.all()
        return {
            "msg": "get ok",
            "status": 200,
            "data": movies,
        }

    @login_admin_required
    def post(self):
        args = parese.parse_args()
        showname = args.get("showname")
        shownameen = args.get("shownameen")
        director = args.get("director")
        leadingRole = args.get("leadingRole")
        type = args.get("type")
        country = args.get("country")
        language = args.get("language")
        duraction = args.get("duraction")
        screeningmodel = args.get("screeningmodel")
        openday = args.get("openday")
        backgroundpicture = args.get("backgroundpicture")
        movie = Movie()
        movie.showname = showname
        movie.shownameen = shownameen
        movie.director = director
        movie.leadingRole = leadingRole
        movie.type = type
        movie.country = country
        movie.language = language
        movie.duraction = duraction
        movie.screeningmodel = screeningmodel
        movie.openday = openday
        print(backgroundpicture.filename)
        file = filename_transfer(backgroundpicture.filename)
        backgroundpicture.save(file[0])  # 文件存储路径
        movie.backgroundpicture = file[1]  # 文件显示路径
        print(file[0], file[1])
        if not movie.save():
            abort(400, msg="can't create  movie")

        return {
            "msg": "post ok",
            "status": 200,
            "data": marshal(movie, movie_fields)
        }


class MovieResource(Resource):

    def get(self, movie_id):
        movie = Movie.query.get(movie_id)
        if not movie:
            abort(404, "该影片不存在")
        return {
            "msg": "get   ok",
            "status": 200,
            "data": marshal(movie, movie_fields)

        }

    @required_admin_permission
    def put(self, movie_id):
        return {"msg": "put  ok"}


if __name__ == "__main__":
    pass
